package com.amazonaws.services.iot.client;


import com.alibaba.fastjson.JSONObject;
import com.siterwell.push.entity.*;
import com.siterwell.push.service.IDeviceLogService;
import com.siterwell.push.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import net.thxjj.sitermodel.Device;

import java.util.List;


@Slf4j
public class TopicListener extends AWSIotTopic {

    private IDeviceLogService iDeviceLogService;

    private IDeviceService iDeviceService;

    public TopicListener(String topic, AWSIotQos qos) {
        super(topic, qos);
    }

    public TopicListener(String topic, AWSIotQos qos, IDeviceLogService iDeviceLogService,IDeviceService iDeviceService) {
        super(topic, qos);
        this.iDeviceLogService = iDeviceLogService;
        this.iDeviceService = iDeviceService;
    }


    @Override
    public void onMessage(AWSIotMessage message) {
        System.out.println(System.currentTimeMillis() + ": <<< " + message.getStringPayload());

        if("{\"IOT\":\"AWS\"}".equals(message.getStringPayload())){
            log.info("网关上线,数据过滤");
            return;
        }

        DataBean dataBean = JSONObject.parseObject(message.getStringPayload(),DataBean.class);
        log.info("收到数据:"+ dataBean.toString());



        try {
            String str1 = dataBean.getMsg().getStr1();

            String str2 = dataBean.getMsg().getStr2();

            String address_str = str1.substring(0,4);

            long address = Long.parseLong(address_str,16);

            String clusterId = str1.substring(4,8);



            String head = str2.substring(0,2);

            String type = str2.substring(10,12);

            if("18".equals(head)&&"42".equals(type)&&"0000".equals(clusterId)){
                String length_str = str2.substring(12,14);
                int leng = Integer.parseInt(length_str,16);
                String prodKey = str2.substring(14,14+leng*2);
                SubDevAddLog devAddLog = new SubDevAddLog()
                        .setDevTid(dataBean.getHw_id())
                        .setAddress(address)
                        .setProdKey(prodKey)
                        .setContent(message.getStringPayload())
                        .setCreatetime(System.currentTimeMillis());

                iDeviceLogService.insertAddSubDevLog(devAddLog);

            }
            else if("09".equals(head) || "19".equals(head)){

                DevLog devLog = new DevLog().setDevTid(dataBean.getHw_id())
                        .setAddress(address)
                        .setClusterId(clusterId)
                        .setContent(message.getStringPayload())
                        .setCreateTime(System.currentTimeMillis());

                iDeviceLogService.insertDevLog(devLog);

                String alarmcontent = "";
                String device = "";
                boolean flag_alarm = false;
                if("09".equals(head)){
                    device = "烟雾报警器"+address_str;
                    GS529BeanEvent event = new GS529BeanEvent(dataBean.getMsg().getStr2());
                    if(event.getAlarm_status()==1){
                        flag_alarm = true;
                        alarmcontent = "烟雾报警";
                    }else if(event.getTest_alarm_status()==1){
                        flag_alarm = true;
                        alarmcontent = "测试报警";
                    }
                }else{
                    device = "水浸报警器"+address_str;
                    if(dataBean.getMsg().getStr2().length()>=10){
                        String status = dataBean.getMsg().getStr2().substring(6,10);
                        if(!"0000".equals(status)){
                            flag_alarm = true;
                            alarmcontent = "报警";
                        }

                    }
                }




               List<String> relationList = iDeviceService.getRelations(dataBean.getHw_id());
               if(relationList.size()>0&&flag_alarm){

               }

            }
            else{
                DevLog devLog = new DevLog().setDevTid(dataBean.getHw_id())
                        .setAddress(address)
                        .setClusterId(clusterId)
                        .setContent(message.getStringPayload())
                        .setCreateTime(System.currentTimeMillis());

                iDeviceLogService.insertDevLog(devLog);
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        if(dataBean!=null){
            String ctrlKey = dataBean.getHw_id() + "_123456";
            String bindKey = dataBean.getHw_id() + "_123456";
            Device device = new Device();
            device.setDeviceId(dataBean.getHw_id());
            device.setCtrlKey(ctrlKey);
            device.setBindKey(bindKey);
            device.setCreateTime(System.currentTimeMillis());
            device.setProdKey(Const.prodkey_of_AWS_gateway);
            iDeviceService.insertGateway(device);
        }


    }


}
